//
//  HRZLoginViewController.m
//  EMarket
//
//  Created by 石头 on 2018/5/7.
//  Copyright © 2018年 好日子. All rights reserved.
//

#import "HRZLoginViewController.h"

// Controllers
#import "HRCRegisteredViewController.h"
// Models

// Views
#import "HRZAccountPsdView.h"
#import "HRZVerificationView.h"
// Vendors

// Categories
#import "UIView+SPExtension.h"

// Others
#import "HRZAuthorService.h"

@interface HRZLoginViewController ()<UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIView *middleLoginView;

/* 上一次选中的按钮 */
@property (strong , nonatomic)UIButton *selectBtn;
/* indicatorView */
@property (strong , nonatomic)UIView *indicatorView;
/* titleView */
@property (strong , nonatomic)UIView *titleView;
/* contentView */
@property (strong , nonatomic)UIScrollView *contentView;

/* 验证码 */
@property (strong , nonatomic)HRZVerificationView *verificationView;
/* 账号密码登录 */
@property (strong , nonatomic)HRZAccountPsdView *accountPsdView;

@end

@implementation HRZLoginViewController

#pragma mark - LazyLoad
- (UIScrollView *)contentView
{
    if (!_contentView) {
        _contentView = [[UIScrollView alloc] initWithFrame:CGRectZero];
        _contentView.showsVerticalScrollIndicator = NO;
        _contentView.showsHorizontalScrollIndicator = NO;
        _contentView.pagingEnabled = YES;
        _contentView.bounces = NO;
        _contentView.delegate = self;
    }
    return _contentView;
}

#pragma mark - LifeCyle

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do view setup here.
    
    [self sertUpBase];
    
    [self setUpTiTleView];
    
    [self setUpContentView];
}

#pragma mark - base
- (void)sertUpBase {
    self.view.backgroundColor = [UIColor whiteColor];
//    _contentView.contentInsetAdjustmentBehavior = false;
//    self.automaticallyAdjustsScrollViewInsets = false;
    
}

#pragma mark - 标题登录
- (void)setUpTiTleView
{
    _titleView = [UIView new];
    _titleView.frame = CGRectMake(0, 0, kScreenWidth, 35);
    [_middleLoginView addSubview:_titleView];
    
    NSArray *titleArray = @[@"账号密码登录",@"短信验证登录"];
    CGFloat buttonW = (_titleView.sp_width - 30) / 2;
    CGFloat buttonH = _titleView.sp_height - 3;
    CGFloat buttonX = 15;
    CGFloat buttonY = 0;
    for (NSInteger i = 0; i < titleArray.count; i++) {
        
        UIButton *button = [UIButton  buttonWithType:UIButtonTypeCustom];
        [button setTitle:titleArray[i] forState:UIControlStateNormal];
        button.titleLabel.font = PFR16Font;
        button.tag = i;
        [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        button.frame = CGRectMake((i * buttonW) + buttonX, buttonY, buttonW, buttonH);
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [_titleView addSubview:button];
    }
    
    UIButton *firstButton = _titleView.subviews[0];
    [self buttonClick:firstButton];
    
    _indicatorView = [UIView new];
    _indicatorView.backgroundColor = [firstButton titleColorForState:UIControlStateSelected];
    
    
    [firstButton.titleLabel sizeToFit];
    
    _indicatorView.sp_height = 2;
    _indicatorView.sp_width = firstButton.titleLabel.sp_width;
    _indicatorView.sp_centerX = firstButton.sp_centerX;
    _indicatorView.sp_y = _titleView.sp_bottom - _indicatorView.sp_height;
    [_titleView addSubview:_indicatorView];
    
    self.contentView.contentSize = CGSizeMake(kScreenWidth * titleArray.count, 0);
}


#pragma mark - 按钮点击
- (void)buttonClick:(UIButton *)button
{
    button.selected = !button.selected;
    [_selectBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    _selectBtn = button;
    
    WEAKSELF
    [UIView animateWithDuration:0.25 animations:^{
        weakSelf.indicatorView.sp_width = button.titleLabel.sp_width;
        weakSelf.indicatorView.sp_centerX = button.sp_centerX;
    }];
    
    CGPoint offset = _contentView.contentOffset;
    offset.x = _contentView.sp_width * button.tag;
    [_contentView setContentOffset:offset animated:YES];
}

#pragma mark - 内容
- (void)setUpContentView
{
    
    self.contentView.backgroundColor = [UIColor orangeColor];
    self.contentView.frame = CGRectMake(0, _titleView.sp_bottom + HRZMargin, kScreenWidth, _middleLoginView.sp_height - _titleView.sp_bottom - HRZMargin);
    [self.middleLoginView addSubview:_contentView];
    
    _verificationView = [HRZVerificationView sp_viewFromXib];
    [_contentView addSubview:_verificationView];
    _accountPsdView = [HRZAccountPsdView sp_viewFromXib];
    [_contentView addSubview:_accountPsdView];
    
    _verificationView.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, _middleLoginView.sp_height - _titleView.sp_height);
    _accountPsdView.frame = CGRectMake(0, 0, kScreenWidth, _middleLoginView.sp_height - _titleView.sp_height);
}

#pragma mark - 注册
- (IBAction)registAccount {
    
    HRCRegisteredViewController *hrzRegistVc = [HRCRegisteredViewController new];
    [self presentViewController:hrzRegistVc animated:YES completion:nil];
    [self.navigationController pushViewController:hrzRegistVc animated:YES];
}

#pragma mark - 退出当前界面
- (IBAction)dismissViewController {
    
    
    [self.view endEditing:YES];
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / scrollView.sp_width;
    UIButton *button = _titleView.subviews[index];
    
    [self buttonClick:button];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

@end
